package org.csu.myPetStore.web.servlet;

import org.csu.myPetStore.domain.Item;
import org.csu.myPetStore.domain.Product;
import org.csu.myPetStore.service.CatalogService;
import org.csu.myPetStore.service.HotService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class MainServlet extends HttpServlet {

    static final String MAIN = "/WEB-INF/jsp/catalog/Main.jsp";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        try {
            RequestDispatcher requestDispatcher;
            requestDispatcher = req.getRequestDispatcher(MAIN);

            HotService service = new HotService();
            String HotProduct = service.getHotGoods();

            HttpSession session = req.getSession();

            if(HotProduct != null){
                session.setAttribute("HotProduct",HotProduct);
                CatalogService catalogService = new CatalogService();
                Item item = catalogService.getItem(HotProduct);
                System.out.println(item.getProduct().getProductId());
                session.setAttribute("item",item);
                session.setAttribute("product",item.getProduct());
            }

             if(requestDispatcher != null)
                requestDispatcher.forward(req,resp);
             else
                 System.out.println("666");
        }catch (Exception e){
            System.out.println("dis为空");
        }

    }
}
